#include "tim2.h"

void tim2_init(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;                                    // 初始化结构
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);                              // 使能TIM2时钟
    TIM_TimeBaseStructure.TIM_Period = clocks.PCLK1_Frequency * 2 / SAMPLE_FREQUENCY; // 采样频率
    TIM_TimeBaseStructure.TIM_Prescaler = 0;                                          // 不分频
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;                                    // TDTS = Tck_tim
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;                       // TIM向上计数模式
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);                                   // 初始化
    TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);                             // 指定TIM2的输出触发源
    TIM_Cmd(TIM2, ENABLE);                                                            // 使能TIM2
}